package org.liuwei.concurent02.demo03_common_pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

public class Main1 {

	public static void main(String[] args) throws Exception {
		GenericObjectPoolConfig conf = new GenericObjectPoolConfig();
		conf.setMaxTotal(4);
		conf.setMinIdle(4);
		final GenericObjectPool<StringBuffer> pool = new GenericObjectPool<StringBuffer>(
				new PooledStringBufferFactory(), conf);

		// pool.returnObject(borrowObject);
		ExecutorService executors = Executors.newFixedThreadPool(4);
		for (int i = 0; i < 4; i++) {

			executors.submit(() -> {
				StringBuffer borrowObject;
				try {
					System.out.println(111);
					borrowObject = pool.borrowObject();
					System.out.println(borrowObject.toString());
					System.out.println(22);
				} catch (Exception e) {
					e.printStackTrace();
				}

			});
		}
	}

}
